<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/16 0016
 * Time: 下午 11:22
 */

namespace app\dlc\controller;
use app\common\model\CaseItem;
use app\dlc\common\helpers\ApiHelper;
use app\dlc\common\helpers\HtmlHelper;
use think\Db;
use QL\QueryList;
class CaseItemController extends CommonController
{
    public function index()
    {
        $this->headNav(CaseItem::$typeList,'type',1);
        $type = input('type_id')?:1;

        $this->headSearch('','name','名称',1);
        $this->headSearch('','cases_id','目标网站',2,Db::name('cases')->column('id,name'));
        $this->headSearch('','type','类型',2,CaseItem::$typeList);
        $this->listBotton('','edit','编辑','/dlc/'.$this->className.'/edit_ajax',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','device','采集规则','/dlc/'.$this->className.'/rule',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','disable','过滤规则','/dlc/'.$this->className.'/filter',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','send','采集测试','/dlc/'.$this->className.'/collection',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','history','执行采集','/dlc/'.$this->className.'/getInfo',4);
        $this->listBotton('','del','删除','/Dlc/'.$this->className.'/_del','1','确定删除');
        if ($type==1){
            $this->listBotton('','enable','重置采集的url','/Dlc/'.$this->className.'/restart','1','确定重置？');
        }else{
            $this->listBotton('','disable','删除该方案的文章','/Dlc/'.$this->className.'/del_article','1','确定删除？');
        }

        $this->loading('index');
        $map = $this->map;
        if ($id = input('id')) $map['id'] = $id;
        unset($map['type_id']);
        $data = CaseItem::withCount('caseItemListUrl,article')->where($map)->order('id desc')->page(self::$page)->select();
        $count = Db::name($this->className)->where($map)->count();
        $this->getPage($count, self::$psize, 'App-loader', $this->title, 'App-search');
        foreach ($data as $key => $value) {
            $data[$key]['typeText'] = CaseItem::$typeList[$value['type']];
            $data[$key]['siteName'] = $value['cases']['name'];
        }
        $this->assign('data',$data);
        $list = ['id'=>'ID','siteName'=>'目标网站','name'=>'名称','cci'=>'文章采集方案','pci'=>'所属列表方案','url'=>'采集的url','case_item_list_url_count'=>'采集的数量','article_count'=>'采集文章的数量','ctime'=>'创建时间','sys'=>'操作'];
        if ($type==2){
            unset($list['case_item_list_url_count']);
            unset($list['cci']);
        }
        if ($type==1){
            unset($list['article_count']);
            unset($list['pci']);
        }
        $this->assign('list',$list);
        echo $this->fetch('common/index');
    }

    public function edit($updata = false, $add = false, $noRepeat = false, $funName = false)
    {
        parent::_edit($updata, $add, $noRepeat, $funName); // TODO: Change the autogenerated stub
        echo $this->fetch('common/edit');
    }

    function del_article(){
        $case_item_id = input('case_item_id');
        if ($case_item_id){
            $res = Db::name('article')->where(['case_item_id'=>$case_item_id])->delete();
            if ($res) admin_return(1,'删除成功');
            admin_return(0,'删除失败');
        }
    }


    public function edit_ajax(){
        $post = input('post.');
        $data['category_id']['data'] =  ApiHelper::getCategory($post['id'])?:[];
        $this->formAddArray = $data;
        $this->_edit_ajax();
    }

    //重置url，采集文章的时候，如果文章删除了，就不能再爬取了，需要重置后才可以再爬
    public function restart(){
        $id = input('id');
        $data = Db::name('case_item_list_url')->where(['case_item_id'=>$id,'status'=>2])->select();
        foreach ($data as $key => $value) {
            $res = Db::name('article')->where(['url'=>$value['url']])->find()?:'';
            if (empty($res)){
                Db::name('case_item_list_url')->where(['id'=>$value['id']])->update(['status'=>1]);
            }
        }
        admin_return(1,'重置成功');
    }
//    设置规则
    public function rule(){
        $this->form = 'case_item_rule';
        $this->_edit_ajax();
    }

//    采集测试
    public function collection(){
        $id = input('id');
        $caseItem = CaseItem::get($id);
        if ($caseItem['category_id']==0&&$caseItem['type']==2){
            echo '请选择接口-分类';die;
        }
        if (strstr($caseItem['url'],'(*)')){
            $str = explode('-',$caseItem['sequence']);
            $str = $str[0];
            $caseItem['url'] = str_replace('(*)',$str,$caseItem['url']);
        }
//        dump($caseItem['url']);die;
        if (!$this->curlPost($caseItem['url'])) {
            echo '网站无法打开';die;
        }
        if ($caseItem['type']==1){//列表采集
            $a = $this->getList($caseItem);
            dump($a);
        }else{//采集内容
            $res = $this->getContent($caseItem);
            dump($res);
            die;
            $arr = ['from','title','keywords','description','content'];
            $rules = [];
            foreach ($arr as $key => $item) {
                $rules[$item] = json_decode($caseItem[$item],true);
            }
            $res = QueryList::Query($caseItem['url'],$rules)->data;

            dump($res[0]);
        }
    }

//    执行采集
    public function getInfo(){
        $id = input('id');
        $caseItem = CaseItem::get($id);
        if ($caseItem['type']==1){//列表采集
            //获取
            if ($caseItem['one_more']==1){//1是单链接    2是多链接
                if (!$this->url_exists($caseItem['url'])) {
                   admin_return(0,'网站打开失败');
                }else{
                    $this->getList($caseItem,1);
                }
            }else{
                $str = explode('-',$caseItem['sequence']);
                for ($i=0;$i<$str[1];$i++){
                    $ss = $i==0?$str[0]:$str[0]+$i*$str[2];
//                    dump($str[0]);
                    $urls[$i] = str_replace('(*)',$ss,$caseItem['url']);
                }
                $caseItem->url = $urls;
                $this->getList($caseItem,1);
            }
        }else{//采集内容
            $this->getContent($caseItem,1);
            die;
        }
        admin_return(1,'采集成功');
    }

    //过滤
    public function filter(){
        echo $this->fetch('common/move');
    }

    //    把链接存到表   $save = false   不存表，只显示出来
    private function getList($caseItem,$save = false){
        $a = [];
        $rules =[
            'link' => ['a:eq(0)','href'],
        ];
        // 切片选择器
        $range = $caseItem['slice_select'];
        //采集某页面所有的链接
        $urls = is_array($caseItem['url'])?$caseItem['url']:[$caseItem['url']];//判断是不是数组，不是的话，变成一个数组-单元素
//        dump($urls);die;
        foreach ($urls as $key => $url) {
            $data = QueryList::Query($url,$rules,$range)->data;
//            网站首页url
            $hostUrl = $caseItem->cases->url;
            foreach ($data as $key => $value) {
                if (strstr($value['link'],'www') || strstr($value['link'],'http')){//如果链接没有域名，这里要添加域名
                    $a[] = $value['link'];
                }else{
                    $a[] = $hostUrl . $value['link'];
//                    $a[] = $value['link'];
                }
            }
        }
        if ($save){
            foreach ($a as $key => $item) {
                $new_data['url'] = $item;
                $new_data['case_item_id'] = $caseItem['id'];
                $new_data['ctime'] = time();
                Db::name('case_item_list_url')->insert($new_data,true);
            }
            admin_return(1,'成功');
        }else{
            return $a;
        }
    }

    //内容
    public function getContent($caseItem,$save = false){
        $arr = ['from','title','keywords','description','content'];
        $rules = [];
        foreach ($arr as $key => $item) {
            $rules[$item] = json_decode($caseItem[$item],true);
        }
        if ($save){

            $pid = $caseItem->pid;
            $data = Db::name('case_item_list_url')->where(['case_item_id'=>$pid,'status'=>1])->select();
            foreach ($data as $key => $value) {
               $res = QueryList::Query($value['url'],$rules)->data;
               $inser = $res[0];
               $inser['content'] = HtmlHelper::filter($inser['content'],$caseItem['id']);
               $inser['from'] = $value['']?:$caseItem->cases->website->name;
               $inser['case_item_id'] = $caseItem['id'];
               $inser['website_id'] = $caseItem->cases->website_id;
               $inser['url'] = $value['url'];
               $inser['case_id'] = $caseItem['cases_id'];
               $inser['category_id'] = $caseItem['category_id'];
               $inser['ctime'] = time();
//               dump($inser);
               //判断该url是否采集过
                $is_url = Db::name('article')->where(['url' => $value['url']])->find();
                if ($is_url){
                    $res = Db::name('article')->insert($inser,true);
                    if ($res) {
                        $up['status'] = 2;
                        Db::name('case_item_list_url')->where(['url'=>$value['url']])->update($up);
                    }
                }

            }
            admin_return(1,'采集成功');
        }else{
            $res = QueryList::Query($caseItem['url'],$rules)->data;
            $data = $res[0];
            $data['content'] = HtmlHelper::filter($data['content'],$caseItem['id']);
//            dump($data);
            return $data;
        }
    }





}
